<?php

namespace Backend\Controllers;

use Models\AppUser;
use Models\Role;
use Models\User;
//use Models\WxImg;

class indexController extends BaseController
{

    public function indexAction()
    {
        $Cuser = $this->Cuser;
//        $Wx = Wx::findFirstByUid($Cuser->uid);
//        $token = $Wx->token;
        if ($Cuser->role_id == 2) {
            $Child = '';
            foreach (User::findByUpUid($Cuser->uid) as $child) {
                $Child .= $child->uid . ",";
            }
            $Child = substr($Child, 0, -1);
            if ($Child != "") {
                $flow_sum = AppUser::sum(array(
                    "column" => "price", "uid in (" . $Child . ")"));
                $Cuser_count = User::count("uid in (" . $Child . ")");
                $this->view->setVar("Cuser_count", $Cuser_count);
                $this->view->setVar("flow_sum", $flow_sum);
                $today_sum = AppUser::sum(array("column" => "price", "uid in (" . $Child . ") AND date(start)=curdate()", 'order' => 'start DESC'));
                $this->view->setVar("today_sum", $today_sum);
                return;
            }
//            $imgcount = WxImg::count("token='" . $token . "'");
//            print_r($imgcount);
        }
        $myappcount = AppUser::count("uid=" . $Cuser->uid . "  AND del=0 AND end>'" . date("Y-m-d H:i:s", strtotime("now")) . "'");
        $appuser = $Cuser->getAppUser("end>'" . date("Y-m-d H:i:s", strtotime("now")) . "' AND del=0");
        $agent_count = User::count("role_id = 2");
        $user_count = User::count();
        $this->view->role = Role::findFirstByroleId($Cuser->role_id);
        $this->view->setVar("agent_count", $agent_count);
        $this->view->setVar("user_count", $user_count);
        $this->view->setVar("Cuser", $Cuser->toArray());
        $this->view->myappcount = $myappcount;
        $this->view->setVar("appuser", $appuser);
    }

}